# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=gmsh
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.7.1
_pkgver2=4_7_1
pkgrel=1
pkgdesc="This is Gmsh, an automatic three-dimensional finite element mesh generator with
built-in pre- and post-processing facilities."
arch=('any')
url="https://gmsh.info"
license=("GPLv2")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-petsc"
             "${MINGW_PACKAGE_PREFIX}-slepc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-med"
         "${MINGW_PACKAGE_PREFIX}-opencascade"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://gitlab.onelab.info/gmsh/gmsh/-/archive/gmsh_${_pkgver2}/gmsh-gmsh_${_pkgver2}.tar.gz
        0001-fix-link-lib-name.patch
        0002-fix-dynamic-linking.patch)
sha256sums=('fcce5de7b4dea573d6cb8133339f62f8f73e23bc99df95ee58836c90eefe8102'
            'eefa684ece808fd705d5abb7804fc2494bdc0fd2b91da8d48378371ec9d6aa5c'
            'f8d23b075fe7f50d2e10e84cd27d7b9c32a8c8b0351012b8331d534f93efd1e4')

prepare() {
  cd "${srcdir}/gmsh-gmsh_${_pkgver2}"

  patch -p1 -i "${srcdir}/0001-fix-link-lib-name.patch"
  patch -p1 -i "${srcdir}/0002-fix-dynamic-linking.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DENABLE_BUILD_DYNAMIC=1 \
      -DENABLE_BUILD_SHARED=1 \
      -DENABLE_OCC=1 \
      -DENABLE_OPENMP=1 \
      -DENABLE_OPENACC=1 \
      -DENABLE_PETSC=1 \
      -DENABLE_SLEPC=1 \
      -DGMSH_RELEASE=1 \
      -DENABLE_CAIRO=1 \
      ../gmsh-gmsh_${_pkgver2}
  
  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install
  
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
}
